"""
应用调试工具 - 在控制台跟踪面板的显示和隐藏状态
"""

import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import QObject, Qt
from app_launcher_fluent import AppLauncherFluent, ShortcutsPanel

class PanelStateTracker(QObject):
    """追踪面板状态的工具类"""
    
    def __init__(self, app_launcher):
        super().__init__()
        self.app_launcher = app_launcher
        self._install_hooks()
        
    def _install_hooks(self):
        """安装钩子函数来监视面板状态变化"""
        # 保存原始方法
        self._orig_toggle = self.app_launcher.toggleShortcutsPanel
        
        # 替换为带跟踪的方法
        def toggle_with_tracking(*args, **kwargs):
            print("\n----- 面板状态变化 -----")
            if hasattr(self.app_launcher, 'shortcutsPanel'):
                is_visible = self.app_launcher.shortcutsPanel.isVisible()
                print(f"当前面板状态: {'可见' if is_visible else '隐藏'}")
                print(f"执行操作: {'隐藏' if is_visible else '显示'}")
            else:
                print("面板未初始化")
            
            # 调用原始方法
            result = self._orig_toggle(*args, **kwargs)
            
            # 显示操作后状态
            if hasattr(self.app_launcher, 'shortcutsPanel'):
                print(f"操作后面板状态: {'可见' if self.app_launcher.shortcutsPanel.isVisible() else '隐藏'}")
            
            return result
            
        # 替换方法
        self.app_launcher.toggleShortcutsPanel = toggle_with_tracking

if __name__ == "__main__":
    # 启动带调试的应用
    app = QApplication(sys.argv)
    app.setAttribute(Qt.AA_UseHighDpiPixmaps)
    
    window = AppLauncherFluent()
    
    # 安装面板状态追踪器
    tracker = PanelStateTracker(window)
    
    # 从main.py导入所需组件
    from config_manager import config
    from main import GlobalHotkeyHandler
    
    # 初始化全局快捷键处理器
    hotkey_handler = GlobalHotkeyHandler(window)
    window.hotkey_handler = hotkey_handler
    
    print("\n===== 面板显示/隐藏调试工具 =====")
    print("快捷键:", config.hotkey)
    print("操作方式:")
    print("1. 按下快捷键切换面板显示/隐藏")
    print("2. 点击悬浮按钮切换面板显示/隐藏")
    print("===============================\n")
    
    window.show()
    sys.exit(app.exec_())
